import uvicorn
from fastapi import FastAPI
from fastapi.middleware.wsgi import WSGIMiddleware
from flask import Flask, request
from markupsafe import escape

# Flask 应用
flaskapp = Flask(__name__)

@flaskapp.route("/")
def flask_main():
    name = request.args.get("name", "Flask")
    return f"Hello, {escape(name)} !"

@flaskapp.route("/safe")
def flask_safe():
    name = request.args.get("name", "Flask")
    # 更安全的做法：使用模板（这里简化演示）
    from markupsafe import Markup
    return Markup(f"<h1>Hello, {escape(name)}!</h1>")

# FastAPI 主应用
app = FastAPI()

@app.get("/app")
def read_main():
    return {"message": "Hello 三酷猫"}

@app.get("/")
def root():
    return {"message": "主应用根路径"}

# 挂载 Flask 应用
app.mount("/flask", WSGIMiddleware(flaskapp))

if __name__ == '__main__':
    uvicorn.run(app=app, host="127.0.0.1", port=8000)